Copyright: Copyright 1996, Berend Tober


Contents
TVector Topics

About TVector

Add Method

Clone Method

Destroy Method

Dot Method

Magnitude Property

MagSquared Property

Normalize Method

Scale Method

Sum Method

TVector Component

Vector Unit

Element Property


Vector Unit

The Vector Unit exports the TVector component which is used to model vectors, i.e., the members of an N-dimensional, real vector space.  Objects of type TVector dynamically allocate memory so that N, the number of elements in the vector, need not be specified at compile time.

You must add the Vector unit to the Uses clause of your module in order to declare TVector type variables.


PG4
Properties

Run-time and read-only

Element

Magnitude

MagSquared


PG6
Methods

Create

Add

Scale

Normalize

Clone

Sum

Dot

Destroy


TVector
TVector Component

Properties Methods

Unit

Vector

Description

The TVector class is used to model vectors, i.e., the members of an N-dimensional, real vector space.  TVector objects dynamically allocate memory so that N, the number of elements in the vector, need not be specified at compile time. Use the Create method to allocate memory for the object. Use the Add method to build the vector component by component at run time. Use the Destroy method to free the resouces occupied by a TVector object.

The class defines methods for performing mathematical operations defined for a vector space, such as scalar multiplication, vector addition, normalization, and dot products. See the help for the corresponding methods at Scale, Sum, Normalize, and Dot. Use the Magnitude property to return the vector length.

Some of your calculations may require the squared length of the vector, rather than the magnitude, and so the MagSquared property is provided to return this directly to avoid your having to square the value returned by the Magitude property. This is because in calculating the magnitude, the squared magnitude is an intermediate result and so the class contains code internally which computes the squared magnitude whenever the Magnitude property is accessed anyway. Thus, your explicity squaring the magintude would be a duplication of effort.

The Clone method creates (allocates memory for) a duplicate of a TVector object and Adds to it the elements of an existing Tvector object. Note that when the clone method is used to assign TVector type variables, the variable must be declared but you should NOT use the create method since the clone method internally executes a create and allocates memory for the clone target. However, you MUST explicitly use the Destroy method to deallocate the resouces of a cloned TVector variable.


PG9
Example

This example adds values from a database file to four different  vectors representing a system of linear equations Wx=y.

var

w: array[1..3] of TVector;

y: TVector;

i: Word;

begin

y := TVector.Create;

for i := 1 to 3 do w[i] := TVector.Create;

while not Table1.Eof do

begin

w[1].Add(Table1SPEED.Value*Table1SPEED.Value);

w[2].Add(Table1SPEED.Value);

w[3].Add(1);

y.Add(Table1GPH.Value/Table1SPEED.Value);

Table1.Next;

end;

y.Destroy;

for i := 1 to 3 do w[i].Destroy;

end;


Add
Add Method

Example

Applies to

TVector

Declaration

function Add(x: Real): Integer;

Description

The Add method appends a new component (ie., and vector element) to a TVector object. Specify the component value you want added to the vector as a Real variable. The method returns the element number to which the component value is assigned.


PG12
Example of Magnitude

var

i:Word;

Vector1: TVector;

Begin

Randomize;

Vector1 := TVector.Create;

for i := 1 to m do Vector1.Add(Random(100));

WriteLn('Initial vectors');

for i := 1 to Vector1.Count do WriteLn(Vector1.Element[i]);

WriteLn('Magnitude1  ',Vector1.Magnitude);

Vector1.Destroy;

end;


Magnitude
Magnitude Property

Example

Applies to

TVector

Declaration

property Magnitude: Real;

Description

The Magnitude property returns the length of a TVector as a Real variable.


PG15
Example of MagSquared

var

i:Word;

Vector1: TVector;

Begin

Randomize;

Vector1 := TVector.Create;

for i := 1 to m do Vector1.Add(Random(100));

WriteLn('Initial vectors');

for i := 1 to Vector1.Count do WriteLn(Vector1.Element[i]);

WriteLn('Squared Magnitude  ',Vector1.MagSquared);

Vector1.Destroy;

end;


MagSquared
MagSquared Property

Example

Applies to

TVector

Declaration

property MagSquared: Real;

Description

The Magnitude property returns the square of the length of a TVector as a Real variable.

This property is provided to avoid your having to square the value returned by the Magnitude property. This is because in calculating the magnitude, the squared magnitude is an intermediate result and so the class contains code internally which computes the squared magnitude whenever the Magnitude property is accessed anyway. Thus, your explicity squaring the magnitude would be a duplication of effort.


PG18
Example of Scale

var

Vector1: TVector;

Begin

Randomize;

Vector1 := TVector.Create;

for i := 1 to m do Vector1.Add(Random(100));

for i := 1 to Vector1.Count do WriteLn(Vector1.Element[i]);

WriteLn('Magnitude1  ',Vector1.Magnitude);

WriteLn;

WriteLn('Scaled vector');

Vector1.Scale(2);

for i := 1 to Vector1.Count do WriteLn(Vector1.Element[i]);

WriteLn('Magnitude1  ',Vector1.Magnitude);

Vector1.Destroy;

end;


Scale
Scale Method

Example

Applies to

TVector

Declaration

procedure Scale(a:Real);

Description

The Scale method performs scalar multiplication on a TVector object, that is, each element of the vector is scaled (multiplied) by the value of Real variable a.


PG21
Example of Normalize

var

i:Word;

Vector1: TVector;

Begin

Randomize;

Vector1 := TVector.Create;

for i := 1 to m do Vector1.Add(Random(100));

for i := 1 to Vector1.Count do WriteLn(Vector1.Element[i]);

WriteLn;

WriteLn('Magnitude1  ',Vector1.Magnitude);

WriteLn;

WriteLn('Normalized vector');

Vector1.Normalize;

for i := 1 to Vector1.Count do WriteLn(Vector1.Element[i]);

WriteLn;

WriteLn('Magnitude1  ',Vector1.Magnitude);

Vector1.Destroy;

end;


Normalize
Normalize Method

Example

Applies to

TVector

Declaration

procedure Normalize;

Description

The Normalize method divides each component of a TVector object by the magnitude of the vector, thus scaling the vector so that it is unit (i.e., magnitude one) vector.


PG24
Example of Clone

var

u, v: TVector;

i: word;

begin

v := TVector.Create;

for i := 1 to 4 do Vector1.Add(Random(100));

u := v.Clone;

u.Destroy;

v.Destroy;

end;


Clone
Clone Method

Example

Applies to

TVector

Declaration

function Clone: TVector;

Description

The Clone method creates (allocates memory for) a duplicate of a TVector object and Adds to it the elements of an existing TVector object. Note that when the clone method is used to assign TVector type variables, the variable must be declared but you should NOT use the create method since the clone method internally executes a create and allocates memory for the clone target. However, you MUST explicitly use the Destroy method to deallocate the resouces of a cloned TVector variable.


PG27
Example of Sum var

i,j:Word;

Vector1, Vector2: TVector;

Begin

Randomize;

Vector1 := TVector.Create;

Vector2 := TVector.Create;

for i := 1 to m do Vector1.Add(Random(100));

for j := 1 to n do Vector2.Add(Random(100));

WriteLn('Initial vectors');

for i := 1 to Vector1.Count do WriteLn(Vector1.Element[i]);

for i := 1 to Vector2.Count do WriteLn(Vector2.Element[i]);

Vector1.Sum(1,Vector2);

WriteLn('Vector sum');

for i := 1 to Vector1.Count do WriteLn(Vector1.Element[i]);

Vector1.Destroy;

end;


Sum
Sum Method

Example

Applies to

TVector

Declaration

procedure Sum(a: Real; v: TVector);

Description

The Sum method adds (component-wise) a  scaled vector to each component of a TVector object.


PG30
Example of Dot var

i,j:Word;

Vector1, Vector2: TVector;

Begin

Randomize;

Vector1 := TVector.Create;

Vector2 := TVector.Create;

for i := 1 to m do Vector1.Add(Random(100));

for j := 1 to n do Vector2.Add(Random(100));

WriteLn('Initial vectors');

for i := 1 to Vector1.Count do WriteLn(Vector1.Element[i]);

for i := 1 to Vector2.Count do WriteLn(Vector2.Element[i]);

writeln(v1.Dot(v2));

Vector1.Destroy;

end;


Dot
Dot Method

Example

Applies to

TVector

Declaration

function Dot(v: TVector): Real;

Description

The Dot method computes the vector dot product of two TVector objects, returning a scalar value.


PG33
Example of Destroy

var

i:Word;

Vector1: TVector;

Begin

Randomize;

Vector1 := TVector.Create;

for i := 1 to m do Vector1.Add(Random(100));

Vector1.Destroy;

end;


Destroy
Destroy Method

Example

Applies to

TVector

Declaration

procedure Destroy;

Description

The Destroy method destroys the TVector object and releases the memory allocated to it. If you construct a TVector object by calling the Create method OR the Clone method, you should call Destroy to release memory and dispose of the object.


Vector
Element Property

Applies to

TVector components

Declaration

property Element[Index: Word]: Real;

Description

Read-only property.

The Element array property lets you access a specific element (or entry, or component) of the vector object. Using the Index parameter of Element you can access a vector component by its position in the vector.


PG35
Create Method

Applies to

All objects

Declaration

constructor Create;

Description

The Create method allocates memory to create the object and initializes its data as needed. Each object can have a Create method customized to create that particular kind of object.


About
About TVector

The TVector component for Delphi was designed and implemented by:

Berend Tober

Compuserve: 70541,1030

Internet:

btober@compuserve.com

btober@connix.com